package util;

import java.util.UUID;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;

public class DataUtil {

	public final static int STORE_STATUS_UNCHECK = -1;
	public final static int STORE_STATUS_CHECK = 0;
	public final static int STORE_STATUS_CHECKED_UNREGIST = 1;
	public final static int STORE_STATUS_CHECKED_REGIST = 2;
	public final static int STORE_STATUS_CHECK_REGISTED = 3;

	/**
	 * 普通admin消息队列
	 */
	public final static int ADMIN_QUEUE = 1;
	/**
	 * 普通store消息队列
	 */
	public final static int STORE_QUEUE = 2;
	/**
	 * 普通amdin主题队列
	 */
	public final static int ADMIN_TOPIC = 3;
	/**
	 * store得到新申请书数量消息队列
	 */
	public final static int STORE_QUEUE_GET_NEWNUMBER = 4;
	public static int STORE_STATUS_SUCCESS = 1;
	public static int STORE_STATUS_FAIL = 0;
	private static MessageConsumer consumer;
	private static Session session;

	public static long getUUID() {

		return UUID.randomUUID().timestamp();

	}
	
	
	
	/**
	 * 普通的admin消息队列
	 */
	private final static Destination queueDestination = new ActiveMQQueue("adminQueue");
	
	/**
	 * 从store消息队列中获得新申请个数信息
	 */
	private final static Destination queueDestinationGetnewNum = new ActiveMQQueue("storeQueueGetnewNum");
	/*private final static Destination queueDestinationGetnewNum = new ActiveMQQueue("77002");*/
	
	/**
	 * 普通的admin主题队列
	 */
	private final static Destination topicDestination = new ActiveMQTopic("adminTopic");
	public static int NEW_NUMBER = 0;
	public static String ReceiveMsg(int msgName) {
		String path = "tcp://115.28.88.171:61616";
		ConnectionFactory factory = new ActiveMQConnectionFactory(path);
		
			Connection conn = null;
			
			try {
				conn = factory.createConnection();
				conn.start();
				session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
				if (msgName == DataUtil.ADMIN_QUEUE) {
					consumer = session.createConsumer(queueDestination);
				}else if(msgName == DataUtil.ADMIN_TOPIC){
					consumer = session.createConsumer(topicDestination);
				}else if (msgName == DataUtil.STORE_QUEUE_GET_NEWNUMBER) {
					consumer = session.createConsumer(queueDestinationGetnewNum);
				}
				TextMessage message =(TextMessage)consumer.receive();
				return message.getText();
			
			}catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				
				try {
					consumer.close();
					session.close();
					conn.close();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		return null;
	}
}
